home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998 April: Mac OS SDK / Dev.CD Apr 98 SDK2.toast / Development Kits (Disc 2) / QuickTime™ VR 2.0 SDK / QTVR C⁄C++ Runtime API / Sample Code / VRShell Sample Code / VRSpeech / Application Files / MacApplication.c < prev    next >
Encoding:
Text File  |  1997-05-22  |  9.3 KB  |  441 lines  |  [TEXT/MPCC]

  1. //
  2. //    File:        MacApplication.c
  3. //
  4. //    Contains:    Functions that could be overridden in a specific application.
  5. //
  6. //    Written by:    Tim Monroe
  7. //                Based (heavily!) on the MovieShell code written by Apple DTS
  8. //
  9. //    Copyright:    © 1994-1996 by Apple Computer, Inc., all rights reserved.
  10. //
  11. //    Change History (most recent first):
  12. //
  13. //       <4>         12/04/96    rtm        added speech recognition support (see TestFunctions.c)
  14. //       <3>         12/02/96    rtm        added cursor updating to DoIdle
  15. //       <2>         11/27/96    rtm        conversion to personal coding style;
  16. //                                    added preliminary QTVR support
  17. //       <1>         12/21/94    khs        first file
  18. //       
  19. //
  20.  
  21. // header files
  22. #include "MacApplication.h"
  23. #include "MacFramework.h"
  24. #include "AppConfiguration.h"
  25. #include "DTSQTUtilities.h"
  26. #include "QTVRUtilities.h"
  27.  
  28. // Header file for the specific test functions.
  29. #include "TestFunctions.h"
  30.  
  31. // global variables
  32. long            gMaxMilliSecToUse = kMaxMilliSecToUse;        
  33. Boolean            gQTVRMgrIsPresent = false;    // is the QuickTime VR Manager available?        
  34. Boolean            gHasSpeechRec;                // is the Speech Recognition Manager available?
  35.  
  36.  
  37.  
  38. //////////
  39. //
  40. // InitApplication
  41. // Do any application-specific initialization.
  42. //
  43. //////////
  44.  
  45. void InitApplication (void)
  46. {
  47.     // make sure that the QTVR Manager is present in the present operating environment
  48.     if (QTVRUtils_IsQTVRMgrInstalled()) {
  49.         gQTVRMgrIsPresent = true;
  50.     } else {
  51.         ShowWarning("\pThe QuickTime VR Manager cannot be found.", 0);
  52.         ExitToShell();
  53.     }
  54.  
  55.     // initialize speech recognition capabilities
  56.     gHasSpeechRec = false;        // assume the worst
  57.     SpeechInit();
  58. }
  59.  
  60.  
  61. //////////
  62. //
  63. // StopApplication
  64. // Do any application-specific shutdown.
  65. //
  66. //////////
  67.  
  68. void StopApplication (void)
  69. {
  70.     // shutdown speech recognition capabilities, if enabled
  71.     if (gHasSpeechRec)
  72.         SpeechStop();
  73. }
  74.  
  75.  
  76. //////////
  77. //
  78. // DoIdle
  79. // Do any processing that can/should occur at idle time.
  80. //
  81. //////////
  82.  
  83. void DoIdle (WindowRef theWindow)
  84. {
  85.     GrafPtr             mySavedPort;
  86.     WindowObject         myWindowObject;
  87.     MovieController        myMC = NULL;
  88.     Point                myPoint;
  89.     
  90.     GetPort(&mySavedPort);
  91.     SetPort(theWindow);
  92.     
  93.     myWindowObject = (WindowObject) GetWRefCon(theWindow);
  94.     if (myWindowObject != NULL) {
  95.     
  96.         myMC = (**myWindowObject).fController;
  97.         if (myMC != NULL) {
  98.         
  99.             MoviesTask(MCGetMovie(myMC), gMaxMilliSecToUse);
  100.  
  101.             // restore the cursor to the arrow if it's outside the front movie window
  102.             // and the mouse button isn't still down;
  103.             // this is needed only for VR movies, but should probably always be done
  104.             if (theWindow == FrontWindow()) {
  105.                 GetMouse(&myPoint);
  106.                 if (!PtInMovie(MCGetMovie(myMC), myPoint) && !StillDown())
  107.                     InitCursor();
  108.             }
  109.         }
  110.     }
  111.  
  112.     // @@@INSERT ANY OTHER IDLE-BASED FUNCTIONALITY HERE
  113.     
  114.     // handle speech-initiated periodics
  115.     if (gHasSpeechRec)
  116.         DoEventLoopSpinCheck();
  117.  
  118.     SetPort(mySavedPort);
  119. }
  120.  
  121.  
  122. //////////
  123. //
  124. // DoUpdateWindow
  125. // Update the specified window.
  126. //
  127. //////////
  128.  
  129. void DoUpdateWindow (WindowRef theWindow, Rect *theRefreshArea)
  130. {
  131.     GrafPtr             mySavedPort;
  132.     
  133.     GetPort(&mySavedPort);
  134.     SetPort(theWindow);
  135.     
  136.     BeginUpdate(theWindow);
  137.  
  138.     EraseRect(theRefreshArea);        // this is important, for non-rectangular movies
  139.     
  140.     // @@@INSERT WINDOW-SPECIFIC DRAWING FUNCTIONALITY HERE
  141.  
  142.     EndUpdate(theWindow);
  143.     SetPort(mySavedPort);
  144. }
  145.  
  146.  
  147. //////////
  148. //
  149. // HandleContentClick
  150. // Handle mouse button clicks in the specified window.
  151. //
  152. //////////
  153.  
  154. void HandleContentClick (WindowRef theWindow, EventRecord *theEvent)
  155. {
  156. #pragma unused(theEvent)
  157.  
  158.     GrafPtr             mySavedPort;
  159.     
  160.     GetPort(&mySavedPort);
  161.     SetPort(theWindow);
  162.  
  163.     // @@@INSERT APPLICATION-SPECIFIC CONTENT CLICKING FUNCTIONALITY HERE
  164.  
  165.     SetPort(mySavedPort);
  166. }
  167.  
  168.  
  169. //////////
  170. //
  171. // HandleQTVRKeyPress
  172. // Handle QuickTime VR-specific key presses.
  173. // Returns true if the key press was handled, false otherwise.
  174. //
  175. //////////
  176.  
  177. Boolean HandleQTVRKeyPress (EventRecord *theEvent)
  178. {
  179.     Boolean        isHandled = true;
  180.     char        myCharCode;
  181.     
  182.     myCharCode = theEvent->message & charCodeMask;
  183.  
  184.     switch (myCharCode) {
  185.  
  186.         default:
  187.             isHandled = false;
  188.             break;
  189.     }
  190.  
  191.     return(isHandled);
  192. }
  193.  
  194.  
  195. //////////
  196. //
  197. // CreateMovieWindow
  198. // Create a window to display a movie in.
  199. //
  200. //////////
  201.  
  202. WindowRef CreateMovieWindow (Rect *theRect, Str255 theTitle)
  203. {
  204.     WindowRef            myWindow;
  205.         
  206.     myWindow = NewCWindow(NULL, theRect, theTitle, false, noGrowDocProc, (WindowPtr) -1L, true, 0);
  207.     return(myWindow);
  208. }
  209.  
  210.  
  211. //////////
  212. //
  213. // HandleApplicationMenu
  214. // Handle selections in the application's menus.
  215. //
  216. //////////
  217.  
  218. void HandleApplicationMenu (short theMenuID, short theMenuItem)
  219. {
  220.     MovieController     myMC = NULL;
  221.     
  222.     switch (theMenuID) {
  223.         case mTesting:
  224.             switch(theMenuItem) {
  225.                 case iTest1:
  226.                     // @@@INSERT YOUR TEST FUNCTION 1 HERE
  227.                     myMC = GetMCFromFrontWindow();
  228.                     if (myMC != NULL) {
  229.                         QTVRUtils_HideControllerBar(myMC);
  230.                     }
  231.                     break;
  232.                 
  233.                 case iTest2:
  234.                     // @@@INSERT YOUR TEST FUNCTION 2 HERE
  235.                     myMC = GetMCFromFrontWindow();
  236.                     if (myMC != NULL) {
  237.                         QTVRUtils_ShowControllerBar(myMC);
  238.                     }
  239.                     break;
  240.                 
  241.                 case iTest3:
  242.                     // @@@INSERT YOUR TEST FUNCTION 3 HERE
  243.                     myMC = GetMCFromFrontWindow();
  244.                     if (myMC != NULL) {
  245.                         QTVRUtils_HideControllerButton(myMC, kQTVRHotSpotButton);
  246.                     }
  247.                     break;
  248.             }
  249.             break;
  250.     }
  251. }
  252.  
  253.  
  254. //////////
  255. //
  256. // AdjustApplicationMenus
  257. // Adjust state of items in the application's menus.
  258. //
  259. //////////
  260.  
  261. void AdjustApplicationMenus (void)
  262. {
  263.     WindowRef             myWindow = NULL;
  264.     MovieController     myMC = NULL;
  265.     
  266.     myMC = GetMCFromFrontWindow();
  267.     if (myMC != NULL) {
  268.         EnableItem(GetMHandle(mTesting), iTest1);
  269.         EnableItem(GetMHandle(mTesting), iTest2);
  270.         EnableItem(GetMHandle(mTesting), iTest3);
  271.     } else {
  272.         DisableItem(GetMHandle(mTesting), iTest1);
  273.         DisableItem(GetMHandle(mTesting), iTest2);
  274.         DisableItem(GetMHandle(mTesting), iTest3);
  275.     }
  276.     
  277.     DisableItem(GetMHandle(mFile), iNew);        // we don't allow creating new VR files here...
  278.     DisableItem(GetMHandle(mFile), iPrint);        // currently printing causes crashes; FIX!
  279. }
  280.  
  281.  
  282. //////////
  283. //
  284. // AddControllerFunctionality
  285. // Configure the movie controller.
  286. //
  287. //////////
  288.  
  289. void AddControllerFunctionality (MovieController theMC)
  290. {
  291.     long            myControllerFlags;
  292.     
  293.     // CLUT table use    
  294.     MCDoAction(theMC, mcActionGetFlags, &myControllerFlags);
  295.     MCDoAction(theMC, mcActionSetFlags, (void *)(myControllerFlags | mcFlagsUseWindowPalette));
  296.  
  297.     // enable keyboard event handling    
  298.     MCDoAction(theMC, mcActionSetKeysEnabled, (void *)true);
  299.     
  300.     // disable drag support
  301.     MCDoAction(theMC, mcActionSetDragEnabled, (void *)false);
  302. }
  303.  
  304.  
  305. //////////
  306. //
  307. // GetQTVRInstanceFromFrontWindow
  308. // Get the QTVRInstance associated with the front window.
  309. //
  310. //////////
  311.  
  312. QTVRInstance GetQTVRInstanceFromFrontWindow (void)
  313. {
  314.     QTVRInstance         myInstance = NULL;
  315.     WindowRef             myWindow = NULL;
  316.     WindowObject        myWindowObject = NULL;
  317.  
  318.     myWindow = FrontWindow();
  319.     if (myWindow == NULL)
  320.         return(NULL);
  321.  
  322.     if (!IsAppWindow(myWindow))
  323.         return(NULL);
  324.             
  325.     myWindowObject = (WindowObject)GetWRefCon(myWindow);
  326.     if (myWindowObject == NULL)
  327.         return(NULL);
  328.         
  329.     MoveHHi((Handle)myWindowObject);
  330.     HLock((Handle)myWindowObject);
  331.  
  332.     // test if this is indeed a movie controller and not an otherwise valid pointer (non-NULL value)
  333.     if (!IsWindowObjectOurs(myWindowObject))
  334.         return(NULL);
  335.         
  336.     myInstance = (**myWindowObject).fInstance;
  337.     HUnlock((Handle)myWindowObject);
  338.     
  339.     return(myInstance);
  340. }
  341.  
  342.  
  343. //////////
  344. //
  345. // InitApplicationWindowObject
  346. // Do any application-specific initialization of the window object.
  347. //
  348. //////////
  349.  
  350. void InitApplicationWindowObject (WindowObject theWindowObject)
  351. {
  352.     Track                myQTVRTrack = NULL;
  353.     OSErr                myErr = noErr;
  354.     Movie                myMovie = NULL;
  355.     MovieController        myMC = NULL;
  356.     QTVRInstance        myInstance = NULL;
  357.         
  358.     if (theWindowObject == NULL)
  359.         return;
  360.  
  361.     // make sure we can safely call the QTVR API
  362.     if (!gQTVRMgrIsPresent)
  363.         return;
  364.  
  365.     // find the QTVR Track
  366.     myMovie = (**theWindowObject).fMovie;
  367.     myMC = (**theWindowObject).fController;
  368.     myQTVRTrack = QTVRGetQTVRTrack(myMovie, 1);
  369.     
  370.     myErr = QTVRGetQTVRInstance(&myInstance, myQTVRTrack, myMC);
  371.     (**theWindowObject).fInstance = myInstance;
  372.  
  373.     // do any QTVR window configuration
  374.     if (myInstance != NULL) {
  375.         // set unit to degrees
  376.         myErr = QTVRSetAngularUnits(myInstance, kQTVRDegrees);
  377.         
  378.         // install a node entering procedure
  379.         myErr = QTVRSetEnteringNodeProc(myInstance, NewEnteringNodeProc(SpeakNameOfNode), (long)myInstance, 0);
  380.         
  381.         // install a few intercept procedures
  382.         // for now, commented out
  383.         //InstallSpeechFeedbackRoutine(myInstance);
  384.     }
  385.     
  386. }
  387.  
  388.  
  389. //////////
  390. //
  391. // RemoveApplicationWindowObject
  392. // Do any application-specific clean-up of the window object.
  393. //
  394. //////////
  395.  
  396. void RemoveApplicationWindowObject (WindowObject theWindowObject)
  397. {
  398.     OSErr                myErr = noErr;
  399.     QTVRInstance        myInstance = NULL;
  400.         
  401.     if (theWindowObject == NULL)
  402.         return;
  403.         
  404.     myInstance = (**theWindowObject).fInstance;
  405.     
  406.     // ***INSERT APPLICATION-SPECIFIC WINDOW-CLOSING CODE HERE***
  407.  
  408.     // DoDestroyMovieWindow in MacFramework.c releases the window object itself
  409. }
  410.  
  411.  
  412. //////////
  413. //
  414. // ApplicationMCActionFilterProc 
  415. // Intercept some mc actions for the QuickTime VR movie controller.
  416. //
  417. //////////
  418.  
  419. pascal Boolean ApplicationMCActionFilterProc (MovieController theMC, short theAction, void *theParams, WindowRef theWindow)
  420. {
  421.     Boolean        isHandled = false;
  422.     
  423.     switch (theAction) {
  424.     
  425.         // handle window resizing
  426.         case mcActionControllerSizeChanged: {
  427.             Rect                myMovieBounds;
  428.  
  429.             MCGetControllerBoundsRect(theMC, &myMovieBounds);
  430.             SizeWindow((WindowPtr)theWindow, myMovieBounds.right - myMovieBounds.left,
  431.                                              myMovieBounds.bottom - myMovieBounds.top, true);
  432.             break;
  433.         }
  434.                         
  435.         default:
  436.             break;
  437.     }
  438.     
  439.     return(isHandled);    
  440. }
  441.